<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;

if (!Session::has('DSL','admin')){
    die('非法登录');
}
class Webpage extends Controller
{
public function index()
    {
        $img_list = db('mainimg')->paginate(10);
        $page = $img_list->render();
        $this->assign('img_list',$img_list);
        $this->assign('page',$page);
        return $this->fetch('index');
    }
    
    public function upload_img()
    {
        $result = array('err' => '1','content' => '');
        $img_id = input('get.img_id');
        $describe = input('get.describe');
        $describe = urldecode($describe);
        $img_id = urldecode($img_id);
        $files = request()->file('img_file');
        $file_name = $files->getInfo();
        $file_name = $file_name['name'];
        $file_name = iconv('utf-8','gb2312',$file_name);
        $info = $files->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'img'.DS.'mainweb',$file_name);
        if ($info->getError()){
            $result['content'] = '上传失败';
            return json_encode($result);
        }
        $mianimg_model = model('Mainimg');
        $img_info = [
            'describe' => $describe,
            'path' => iconv('gb2312','utf-8', $info->getSaveName()),
        ];
        if ($img_id != ''){
            $img_info['id'] = $img_id;
        }
        $save_img = $mianimg_model->SaveMainimg($img_info);
        if ($save_img == false){
            $result['content'] = '保存失败！';
            return json_encode($result);
        }
        $result['err'] = '0';
        $result['content'] = '上传成功！';
        return json_encode($result);
    }
    
    public function del_img()
    {
        $result = array('err'=>'1','content' => '');
        $img_id = input('post.id');
        $img_path = db('mainimg')->where('id',$img_id)->value('path');
        $img_path = iconv('utf-8','gb2312',$img_path);
        unlink(ROOT_PATH.'public'.DS.'static'.DS.'img'.DS.'mainweb'.DS.$img_path);
        $del_resource = db('mainimg')->where('id',$img_id)->delete();
        if ($del_resource == false){
            $result['content'] = '删除失败！'.$img_id;
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '删除成功！';
        return $result;
    }
    
    public function edit_img()
    {
        $img_id = input('get.img_id');
        $img = db('mainimg')->where('id',$img_id)->find();
        $this->assign('img',$img);
        return $this->fetch('edit');
    }
    
    public function edit_describe()
    {
        $result = array('err' => '1','content' => '');
        $img_id = input('post.img_id');
        $describe = input('post.describe');
        $model = model('Mainimg');
        $save = $model->saveDescribe($describe,$img_id);
        if ($save == false){
            $result['content'] = '保存失败';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '保存成功！';
        return $result;
        
        
    }
}